package com.taojie.graduation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taojie.graduation.domain.Car;
import com.taojie.graduation.domain.User;
import com.taojie.graduation.mapper.CarMapper;
import com.taojie.graduation.service.api.CarService;
import com.taojie.graduation.utils.PageResult;
import com.taojie.graduation.utils.Result;
import com.taojie.graduation.utils.ResultCode;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CarServiceImpl implements CarService {
    @Resource
    CarMapper carMapper;

    /**
     * TODO 分页查
     * @param page
     * @param size
     * @param query
     * @return
     */
    @Override
    public Result findCarPage(int page, int size, String query) {
        Page<Car> userPage = new Page<>(page, size);
        QueryWrapper<Car> wapper = null;
        if (!StringUtils.isEmpty(query)) {
            wapper = new QueryWrapper<>();
            wapper.like("name", query);
        }
        Page<Car> carPage1 = carMapper.selectPage(userPage, wapper);
        // 根据id排序
        List<Car> collect = carPage1.getRecords().stream().sorted(Comparator.comparing(Car::getId)).collect(Collectors.toList());
        if (!(carPage1.getRecords().size() > 0)) {
            return new Result(ResultCode.FAIL);
        }
        return new Result(ResultCode.SUCCESS, new PageResult<Car>(carPage1.getTotal(), collect));
    }

    /**
     * TODO 根据id查
     * @param id
     * @return
     */
    @Override
    public Result findCarById(Integer id) {
        if (id < 0) { return  Result.INPUT_ERROR(); }
        Car car = carMapper.selectById(id);
        if (ObjectUtils.isEmpty(car)){
            return Result.FAIL();
        }
        return new Result(ResultCode.SUCCESS, car);
    }

    /**
     * TODO 更新
     * @param car
     * @return
     */
    @Override
    public Result updateById(Car car) {
        if (car.getId() < 0) {
            return Result.INPUT_ERROR();
        }
        int i = carMapper.updateById(car);
        if (i <= 0) {
            return Result.FAIL();
        }
        return Result.SUCCESS();
    }

    /**
     * TODO 根据id删除
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Integer id) {
        if (id < 0) {
            return Result.INPUT_ERROR();
        }
        int i = carMapper.deleteById(id);
        if (i <= 0) {
            return Result.FAIL();
        }
        return Result.SUCCESS();
    }

    /**
     * TODO 新增
     * @param car
     * @param user 用户
     * @return
     */
    @Override
    public Result add(Car car, User user) {
        System.out.println(LocalDateTime.now());
        if (user.getId() < 0) {
            return Result.INPUT_ERROR();
        }
        // 设置购物车所属用户和购物车名称
        car.setUserId(user.getId());
        car.setName(user.getNickName() + "的购物车");
        int i = carMapper.insert(car);
        if (i <= 0) {
            return Result.FAIL();
        }
        return Result.SUCCESS();
    }

    /**
     * TODO 根据用户id删除购物车
     * @param id
     * @return
     */
    @Override
    public Result deleteByUserId(Integer id) {
        QueryWrapper<Car> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", id);
        int i = carMapper.delete(wrapper);
        if (i < 0) {
            return Result.FAIL();
        }
        return Result.SUCCESS();
    }

    /**
     * TODO 根据用户id查询购物车
     * @param id
     * @return
     */
    @Override
    public Car findByUserId(Integer id) {
        QueryWrapper<Car> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", id);
        return carMapper.selectOne(wrapper);
    }
}
